package com.tjetc.service.impl;

import com.tjetc.constant.CommonConstant;
import com.tjetc.dao.BookingDao;
import com.tjetc.dao.ExcelDao;
import com.tjetc.dao.GuestDao;
import com.tjetc.dao.OrderDao;
import com.tjetc.dao.impl.BookingDaoImpl;
import com.tjetc.dao.impl.ExcelDaoImpl;
import com.tjetc.dao.impl.GuestDaoImpl;
import com.tjetc.dao.impl.OrderDaoImpl;
import com.tjetc.exception.MessageException;
import com.tjetc.pojo.entity.Booking;
import com.tjetc.pojo.entity.Income;
import com.tjetc.pojo.entity.Order;
import com.tjetc.pojo.vo.BookingVO;
import com.tjetc.pojo.vo.CountVO;
import com.tjetc.pojo.vo.OrderVO;
import com.tjetc.service.ExcelService;
import com.tjetc.utils.ExcelUtil;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Author: itda
 * @Date: 2024/5/11 21:57
 * @Description: ExcelServiceImpl
 */
public class ExcelServiceImpl implements ExcelService {

    private ExcelDao excelDao = new ExcelDaoImpl();

    private BookingDao bookingDao = new BookingDaoImpl();

    private GuestDao guestDao = new GuestDaoImpl();

    private OrderDao orderDao = new OrderDaoImpl();

    @Override
    public void today() {
        try {
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime start = now.withHour(0).withMinute(0).withSecond(0).withNano(0);
            LocalDateTime end = now.withHour(23).withMinute(59).withSecond(59).withNano(999_999_999);
            List<Income> Costs = excelDao.getCostByTime(start, end);
            List<String> headers = Arrays.asList("房间id", "收入","创建时间");
            List<List<Object>> data = new ArrayList<>();
            Costs.forEach(s -> {
                data.add(Arrays.asList(s.getRoomId(), s.getCost(),s.getCreateTime()));
            });
            String url = UUID.randomUUID() +
                    "-" +
                    LocalDate.now() +
                    "房间收入表.xlsx";
            ExcelUtil.exportToExcel(url, headers, data, LocalDate.now() + "日房间收入账单");
            System.out.println(CommonConstant.EXPORT_DATA_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void month() {
        try {
            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            // 获取本月开始时间
            LocalDateTime start = currentDate.withDayOfMonth(1).atStartOfDay();
            // 获取本月结束时间
            LocalDateTime end = currentDate.withDayOfMonth(currentDate.lengthOfMonth()).atTime(LocalTime.MAX);
            List<Income> Costs = excelDao.getCostByTime(start, end);
            List<String> headers = Arrays.asList("房间id", "收入","创建时间");
            List<List<Object>> data = new ArrayList<>();
            Costs.forEach(s -> {
                data.add(Arrays.asList(s.getRoomId(), s.getCost(),s.getCreateTime()));
            });
            String url = UUID.randomUUID() +
                    "-" +
                    LocalDate.now().getYear() +
                    "-" +
                    LocalDate.now().getMonthValue() +
                    "月收入表.xlsx";
            ExcelUtil.exportToExcel(url, headers, data, LocalDate.now().getMonthValue() + "月房间收入账单");
            System.out.println(CommonConstant.EXPORT_DATA_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void booking() {
        try {
            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            // 获取本月开始时间
            LocalDateTime start = currentDate.withDayOfMonth(1).atStartOfDay();
            // 获取本月结束时间
            LocalDateTime end = currentDate.withDayOfMonth(currentDate.lengthOfMonth()).atTime(LocalTime.MAX);
            //List<BookingVO> list = bookingDao.getBookingByTime(start, end);
            List<OrderVO> list = orderDao.getOrderByTime(start, end);
            List<String> headers = Arrays.asList("房间id", "顾客id", "姓名", "邮箱", "手机号", "消费金额", "预定时间", "退房时间", "订单状态");
            List<List<Object>> data = new ArrayList<>();
            list.forEach(s -> {
                if (s.getCheckInDate() == null || s.getCheckOutDate()==null) {
                    data.add(Arrays.asList(s.getRoomId(), s.getGuestId(), s.getUsername(), s.getEmail(), s.getPhone(), s.getTotalPrice(),
                            "没入住", "没入住", s.getStatus()));
                } else {
                    data.add(Arrays.asList(s.getRoomId(), s.getGuestId(), s.getUsername(), s.getEmail(), s.getPhone(), s.getTotalPrice(),
                            s.getCheckInDate(), s.getCheckOutDate(), s.getStatus()));
                }
            });
            String url = UUID.randomUUID() +
                    "-" +
                    LocalDate.now().getYear() +
                    "-" +
                    LocalDate.now().getMonthValue() +
                    "月订单统计表.xlsx";
            ExcelUtil.exportToExcel(url, headers, data, LocalDate.now().getMonthValue() + "月订单统计");
            System.out.println(CommonConstant.EXPORT_DATA_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /*public void earnCount() {
        try {
            LocalDateTime now = LocalDateTime.now();
            List<CountVO> Costs = excelDao.getCountCostByTime(now);
            System.out.println(CommonConstant.EXPORT_DATA_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }*/
}
